package org.vt.ece3574.pictoslider.puzzle;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

/**
 * This class represents a piece to be placed within a Puzzle object.
 * 
 * @author Michael Drescher
 */
public class PuzzlePiece extends ImageView {

	/**
	 * Returns true if the PuzzlePiece is able to be moved, false otherwise
	 */
	private boolean moveable;
	/**
	 * The current row that this piece resides in.
	 */
	private int row;
	/**
	 * The current column that this piece resides in.
	 */
	private int column;
	/**
	 * The correct row for this piece.
	 */
	private int startingRow;
	/**
	 * The correct column for this piece.
	 */
	private int startingColumn;

	/**
	 * Constructs a new PuzzlePiece with the image referenced by b.
	 * 
	 * @param context
	 *            - The context for the PuzzlePiece
	 * @param b
	 *            - The Bitmap to be placed within the PuzzlePiece
	 * @param r
	 *            - The row that this puzzle piece is located in
	 * @param c
	 *            - The column that this puzzle piece is located in
	 */
	public PuzzlePiece(Context context, Bitmap b, int r, int c) {
		super(context);
		setImageBitmap(b);
		moveable = false;
		row = r;
		column = c;
		startingRow = r;
		startingColumn = c;
	}

	/**
	 * @return True if the PuzzlePiece is able to be moved.
	 */
	public boolean isMoveable() {
		return moveable;
	}

	/**
	 * Sets whether a PuzzlePiece is susceptible to motion events or not.
	 * 
	 * @param moveable
	 *            - True if the piece should be moveable, false if not.
	 */
	public void setMoveable(boolean moveable) {
		this.moveable = moveable;
	}

	/**
	 * @return True if the PuzzlePiece is the blank piece.
	 */
	public boolean isBlankPiece() {
		return (getVisibility() == INVISIBLE);
	}

	/**
	 * @return The Puzzle which this piece belongs to.
	 */
	public Puzzle getPuzzle() {
		return (Puzzle) getParent().getParent();
	}

	/**
	 * @return True if the piece is in its correct position, false otherwise.
	 */
	public boolean checkIfInPosition() {
		return ((row == startingRow) && (column == startingColumn));
	}

	/**
	 * @param r
	 *            - The current row the piece resides in.
	 */
	protected void setRow(int r) {
		row = r;
	}

	/**
	 * @param c
	 *            - The current column the piece resides in.
	 */
	protected void setColumn(int c) {
		column = c;
	}

	/**
	 * @return The current row the piece resides in.
	 */
	public int getRow() {
		return row;
	}

	/**
	 * @return The current column the piece resides in.
	 */
	public int getColumn() {
		return column;
	}

}
